package com.uye.framework.util;

import java.util.ResourceBundle;

/**
 * Loads and stores entries in errorCodes.properties for easy access throughout
 * the application.
 * 
 * @author uye
 * 
 */
public final class ResponseCodes {

	/**
	 * File name of the properties file. Must be in the classpath root.
	 */
	private static final String BUNDLE_NAME = "errorCodes";

	/**
	 * Resource bundle for the property file.
	 */
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle(BUNDLE_NAME);

	/**
	 * The default associated description for unknown error codes. Error codes
	 * and their descriptions should really be declared in the
	 * errorCode.properties file.
	 */
	private static final String UNKNOWN_ERROR_CODE_DESCRIPTION = "Unknown error code.";

	/**
	 * Convenience enum to access the common errors expected by the application.
	 * Note that errors
	 * 
	 * @author uye
	 * 
	 */
	// TODO streamline error management and reporting.
	public static enum BASE_ERROR_CODES {

		/**
		 * Expected entries.
		 */
		ERROR_N1("-1"), ERROR_503("503"), ERROR_500("500"), ERROR_404("404"), ERROR_400(
				"400"), ERROR_50001("50001");

		/**
		 * The error code.
		 */
		private String errorCode;

		/**
		 * Constructor accepting/setting the String representation of the error
		 * code.
		 * 
		 * @param errorCode
		 *            The associated String representation of the error code
		 */
		private BASE_ERROR_CODES(final String errorCode) {
			this.errorCode = errorCode;
		}

		/**
		 * Overrides the default toString() method to display the error code.
		 * 
		 * @return String representation of the error code
		 */
		@Override
		public String toString() {
			return errorCode;
		}
	}

	/**
	 * Prevent instantiation of this helper class.
	 */
	private ResponseCodes() {
	}

	/**
	 * Retrieves the appropriate description for a given error code.
	 * 
	 * @param errorCode
	 *            The String representation of the error code.
	 * @return The error code's description
	 */
	public static String getDescription(String errorCode) {
		if (hasDescription(errorCode)) {
			return RESOURCE_BUNDLE.getString(errorCode);
		} else {
			return getDescription(UNKNOWN_ERROR_CODE_DESCRIPTION);
		}
	}

	/**
	 * Retrieves the appropriate description for a given error code.
	 * 
	 * @param errorCode
	 *            An enum entry representation of the error code. The
	 *            description will be retrieved based on the enum's toString()
	 *            method. Make sure that the appropriate behaviour is
	 *            implemented before passing it here.
	 * @return The error code's description
	 */
	public static String getDescription(final Enum errorCode) {
		return RESOURCE_BUNDLE.getString(errorCode.toString());
	}

	/**
	 * Verifies if a given error code has been given a description in the
	 * properties file.
	 * 
	 * @param errorCode
	 * @return
	 */
	public static boolean hasDescription(String errorCode) {
		return RESOURCE_BUNDLE.containsKey(errorCode);
	}
}
